;Make a DM, with 57 actuators, similar to the mono61 design.

function make_dm61, posns, psize, m_pix, projmat=projmat

if (n_elements(posns) ne 37) then return, -1
p = posns - mean(posns)
edge = 0.5*(p[19:36] + shift(p[19:36],-1))
q = [p,edge]
x = fltarr(37)
y = fltarr(37)
sep =(1/7.)/m_pix
angles =( 60*findgen(6))*!pi/180.
for i=0,5 do x[i+1]= sep*cos(angles[i])
for i=0,5 do y[i+1]= sep*sin(angles[i])
angles =( 30*findgen(12))*!pi/180.
for i=0,11 do x[i+7]= 2*sep*cos(angles[i])
for i=0,11 do y[i+7] =2*sep*sin(angles[i])
angles =( 20*findgen(18))*!pi/180.
for i=0,17 do x[i+19]= 3*sep*cos(angles[i])
for i=0,17 do y[i+19] =3*sep*sin(angles[i])
;;Next we optionally find a matrix that projects the DM onto the first
;;6 zernike modes (excluding piston)
if (keyword_set(projmat)) then begin
 tfa_project = [[x/sep],[y/sep],[(x^2+y^2)/sep^2], [(x^2-y^2)/sep^2], [x*y/sep^2]]
 nv = (size(tfa_project))[2]
 norm = sqrt(total(tfa_project^2,1))
 norm = transpose(rebin(norm,nv,37))
 tfa_onorm = tfa_project
 for i=1,nv-1 do begin
     for j=0,i-1 do tfa_onorm[*,i] -= total(tfa_onorm[*,j]*tfa_project[*,i])*tfa_onorm[*,j]
 endfor
 norm = sqrt(total(tfa_onorm^2,1))
 norm = transpose(rebin(norm,5,37))
 tfa_onorm /= norm
 projmat = tfa_onorm # transpose(tfa_onorm)
end
x = [x,replicate(0,18)]
y = [y,replicate(0,18)]
angles += 10*!pi/180.
for i=0,17 do x[i+37] = sep*4.4*cos(angles[i])
for i=0,17 do y[i+37] = sep*4.4*sin(angles[i])


x += psize/2
y += psize/2
triangulate,x, y, triangles
dm = trigrid(x,y,q,triangles,[1,1], [0,0,psize-1,psize-1])
return, dm
end
